<?php

class Action
{

    /**
     * 检查所有合伙人的抱抱币
     * 当前值 <=> 流水汇总值 是否相同
     */
    public function check_all_dealer_bbcoin() {
        $dealers = array_to_filelds_assoc('id', user_helper::get_all_dealers_info());

        $dealers_ids = array_keys($dealers);

        $SQL = 'SELECT ' .
               join(', ', array(
                   'bbr.`user_id`',
                   'SUM(bbr.`bbcoin`) \'bbcoin_sum\'',
               )) .
               ' FROM `baobaotang`.`bbcoin_record` bbr' .
               ' WHERE bbr.`user_id` IN(' .
               join(',', $dealers_ids) .
               ')' .
               ' GROUP BY bbr.`user_id`';

        $dealers_bbcoin_sum = array_to_filelds_assoc('user_id', run_sql('bbcoin_record', 'getAll', $SQL));

        echo 'ID : 姓名 : 当前抱抱币 : 流水汇总抱抱币<br/>';

        foreach ($dealers as $dealer_id => $dealer_info) {
            $bbcoin_sum = isset($dealers_bbcoin_sum[$dealer_id]) ? $dealers_bbcoin_sum[$dealer_id]['bbcoin_sum'] : 0;

            if (intval($bbcoin_sum) != intval($dealer_info['bbcoin'])) {
                echo $dealer_info['id'] .
                     ' : ' .
                     $dealer_info['realname'] .
                     ' : ' .
                     $dealer_info['bbcoin'] .
                     ' : ' .
                     $bbcoin_sum .
                     ' : ' .
                     '<a href="' .
                     AnUrl('tools/datafix/list_dealer_bbcoin_orders_and_records') .
                     '?user_id=' .
                     $dealer_info['id'] .
                     '">流水列表</a>' .
                     '<br/>';
            }
        }
    }

    /**
     * 列出某位合伙人的抱抱币出入账流水
     */
    public function list_dealer_bbcoin_orders_and_records() {

        $user_id = Request::Get('user_id', 0);

        if ($user_id == 0) {
            return '无效的用户';
        }

        if (user_config::AGENCY != user_helper::get_user_role_id($user_id)) {
            return '用户不是很合伙人';
        }

        $res_type_index = bbcoin_config::res_type_index();

        $records = _model('bbcoin_record')->getList(array('user_id' => $user_id), ' ORDER BY `add_time` DESC');

        echo '<style>table{border-collapse:collapse}td{border:1px solid #666666;padding:5px;}</style>';

        echo '<table><tr><td> 记录汇总: ' . array_sum(array_column($records, 'bbcoin')) . '</td></tr></table>';
        echo '<table>';
        foreach ($records as $record) {
            echo '<tr><td>' . join('</td><td>', array(
                    $record['id'],
                    $record['order_id'],
                    $record['add_time'],
                    $record['bbcoin'],
                    $res_type_index[$record['res_id']]['res_name'],
                )) . '</td></tr>';
        }
        echo '</table>';

        $payed_orders = _model('order')->getList(array(
            'user_id' => $user_id,
        ), ' ORDER BY `add_time` DESC');

        echo '<hr/>';

        echo '<table><tr><td> 消耗汇总: ' . array_sum(array_column($payed_orders, 'pay_bbcoin')) . '</td></tr></table>';
        echo '<table>';
        foreach ($payed_orders as $order) {
            echo '<tr><td>' .
                 join('</td><td>', array($order['id'], $order['add_time'], $order['pay_bbcoin'])) .
                 '</td></tr>';
        }
        echo '</table>';

        $receive_orders_delivery = run_sql('order_delivery_note', 'getAll', 'SELECT ' .
                                                                            join(',', array(
                                                                                'o.`id`',
                                                                                'odn.`return_bbcoin`',
                                                                                'odn.`return_bbcoin_time`',
                                                                                'o.`progress`'
                                                                            )) .
                                                                            ' FROM `order_delivery_note` odn' .
                                                                            ' LEFT JOIN `order` o ON o.`id`=odn.`order_id`' .
                                                                            ' WHERE o.`agency_id`=\'' .
                                                                            $user_id .
                                                                            '\' AND o.`progress` != 15' .
                                                                            ' AND odn.`return_bbcoin_time` != \'0000-00-00 00:00:00\'' .
                                                                            ' ORDER BY odn.`return_bbcoin_time` DESC');

        $receive_orders_exchange = run_sql('order', 'getAll', 'SELECT ' .
                                                              join(',', array(
                                                                  'o.`id`',
                                                                  'o.`pay_bbcoin`',
                                                                  'o.`receiver_time`',
                                                                  'o.`progress`'
                                                              )) .
                                                              ' FROM `order` o' .
                                                              ' WHERE o.`agency_id`=\'' .
                                                              $user_id .
                                                              '\' AND o.`progress` != 15' .
                                                              ' AND o.`pay_bbcoin` > 0' .
                                                              ' ORDER BY o.`receiver_time` DESC');

        $receive_orders_cinema_bulk = run_sql('order_branch', 'getAll', 'SELECT ' .
                                                                        join(',', array(
                                                                            'o.`id`',
                                                                            'ob.`bbcoin`',
                                                                            'o.`receiver_time`',
                                                                            'o.`progress`'
                                                                        )) .
                                                                        ' FROM `order_branch` ob' .
                                                                        ' LEFT JOIN `order` o ON o.`id`=ob.`order_id`' .
                                                                        ' LEFT JOIN `store` s ON o.`store_id`=s.`id`' .
                                                                        ' WHERE o.`agency_id`=\'' .
                                                                        $user_id .
                                                                        '\' AND o.`progress` != 15' .
                                                                        ' AND s.`type` = 100' .
                                                                        ' AND ob.`bbcoin` > 0' .
                                                                        ' ORDER BY o.`receiver_time` DESC');

        echo '<hr/>';
        echo '<table><tr><td> 进账汇总: ' .
             (array_sum(array_column($receive_orders_delivery, 'return_bbcoin')) +
              array_sum(array_column($receive_orders_exchange, 'pay_bbcoin')) +
              array_sum(array_column($receive_orders_cinema_bulk, 'bbcoin'))) .
             '</td></tr></table>';

        echo '<table><tr><td>白条返还的</td></tr></table>';
        echo '<table>';
        foreach ($receive_orders_delivery as $order) {
            echo '<tr><td>' . join('</td><td>', array(
                    $order['id'],
                    $order['return_bbcoin_time'],
                    $order['return_bbcoin'],
                    $order['progress']
                )) . '</td></tr>';
        }
        echo '</table>';
        echo '<table><tr><td>终端兑换</td></tr></table>';
        echo '<table>';
        foreach ($receive_orders_exchange as $order) {
            echo '<tr><td>' . join('</td><td>', array(
                    $order['id'],
                    $order['receiver_time'],
                    $order['pay_bbcoin'],
                    $order['progress']
                )) . '</td></tr>';
        }
        echo '</table>';
        echo '<table><tr><td>影院散装进货奖励合伙人</td></tr></table>';
        echo '<table>';
        foreach ($receive_orders_cinema_bulk as $order) {
            echo '<tr><td>' . join('</td><td>', array(
                    $order['id'],
                    $order['receiver_time'],
                    $order['bbcoin'],
                    $order['progress']
                )) . '</td></tr>';
        }
        echo '</table>';
    }

    /**
     * 检查所有合伙人的余额
     * 当前值 <=> 流水汇总值 是否相同
     */
    public function check_all_dealer_user_money() {
        $dealers = array_to_filelds_assoc('id', user_helper::get_all_dealers_info());

        $dealers_ids = array_keys($dealers);

        $SQL = 'SELECT ' .
               join(', ', array(
                   'umr.`user_id`',
                   'SUM(umr.`user_money`) \'user_money_sum\'',
               )) .
               ' FROM `baobaotang`.`user_money_record` umr' .
               ' WHERE umr.`user_id` IN(' .
               join(',', $dealers_ids) .
               ')' .
               ' GROUP BY umr.`user_id`';

        $dealers_user_money_sum = array_to_filelds_assoc('user_id', run_sql('user_money_record', 'getAll', $SQL));

        echo 'ID : 姓名 : 当前余额 : 流水汇总余额<br/>';

        foreach ($dealers as $dealer_id => $dealer_info) {
            $user_money_sum = isset($dealers_user_money_sum[$dealer_id])
                ? $dealers_user_money_sum[$dealer_id]['user_money_sum'] : 0;

            if (intval($user_money_sum) != intval($dealer_info['user_money'])) {
                echo $dealer_info['id'] .
                     ' : ' .
                     $dealer_info['realname'] .
                     ' : ' .
                     $dealer_info['user_money'] .
                     ' : ' .
                     $user_money_sum .
                     '<a href="' .
                     AnUrl('tools/datafix/list_dealer_user_money_orders_and_records') .
                     '?user_id=' .
                     $dealer_info['id'] .
                     '">流水列表</a>' .
                     '<br/>';
            }
        }
    }

    /**
     * 列出某位合伙人的余额出入账流水
     */
    public function list_dealer_user_money_orders_and_records() {

        $user_id = Request::Get('user_id', 0);

        if ($user_id == 0) {
            return '无效的用户';
        }

        if (user_config::AGENCY != user_helper::get_user_role_id($user_id)) {
            return '用户不是很合伙人';
        }

        $res_type_index = user_money_config::res_type_index();

        $records = _model('user_money_record')->getList(array('user_id' => $user_id), ' ORDER BY `add_time` DESC');

        echo '<style>table{border-collapse:collapse}td{border:1px solid #666666;padding:5px;}</style>';

        echo '<table><tr><td> 记录汇总: ' . array_sum(array_column($records, 'user_money')) . '</td></tr></table>';
        echo '<table>';
        foreach ($records as $record) {
            echo '<tr><td>' . join('</td><td>', array(
                    $record['id'],
                    $record['order_id'],
                    $record['add_time'],
                    $record['user_money'],
                    $res_type_index[$record['res_id']]['res_name'],
                )) . '</td></tr>';
        }
        echo '</table>';

        $payed_orders = _model('order')->getList(array(
            'user_id' => $user_id,
        ), ' ORDER BY `add_time` DESC');

        echo '<hr/>';

        echo '<table><tr><td> 消耗汇总: ' . array_sum(array_column($payed_orders, 'pay_user_money')) . '</td></tr></table>';
        echo '<table>';
        foreach ($payed_orders as $order) {
            echo '<tr><td>' .
                 join('</td><td>', array($order['id'], $order['add_time'], $order['pay_user_money'])) .
                 '</td></tr>';
        }
        echo '</table>';

        $receive_orders = run_sql('order_delivery_note', 'getAll', 'SELECT o.`id`,odn.`return_cash`,odn.`return_cash_time`,o.`progress` FROM `order_delivery_note` odn LEFT JOIN `order` o ON o.`id`=odn.`order_id` WHERE o.`agency_id`=\'' .
                                                                   $user_id .
                                                                   '\' AND o.`progress` != 15 ORDER BY odn.`return_cash_time` DESC');

        echo '<hr/>';
        echo '<table><tr><td> 进账汇总: ' . array_sum(array_column($receive_orders, 'return_cash')) . '</td></tr></table>';

        echo '<table>';
        foreach ($receive_orders as $order) {
            echo '<tr><td>' . join('</td><td>', array(
                    $order['id'],
                    $order['return_cash_time'],
                    $order['return_cash'],
                    $order['progress']
                )) . '</td></tr>';
        }
        echo '</table>';
    }

    // 分类迁移
    public function goods_category() {
        return false;
        //        $list = _model('category')->getList(array(
        //            'res_name' => 'goods'
        //        ));
        //        var_dump($list);
        //        if (0 == _model('goods_category')->getTotal(array('1' => 1))) {
        //            foreach ($list as $item) {
        //                _model('goods_category')->create(parse_param(array_fill_keys(array(
        //                    'id',
        //                    'title',
        //                    'parent_id',
        //                    'status',
        //                    'add_time',
        //                ), ''), $item));
        //            }
        //            var_dump('success!');
        //        }
    }

    // 移动门店抱抱币/余额至门店的第一分店
    public function store_money_bbcoin() {

        return false;
        /*
                $verify = Request::Get('verify', 0);

                $first_store = _model('store')->getList(array('store_order' => '1', 'status > ' => '0', 'is_enable' => '1'));

                foreach ($first_store as $item) {
                    $user_info = _uri('user', $item['owner_id']);
                    if ($user_info['bbcoin'] > 0 || $user_info['user_money'] > 0) {
                        var_dump($user_info['id'], $user_info['bbcoin'], $user_info['user_money']);
                        if ($verify == 1) {
                            _model('user')->update($item['user_id'], array(
                                'bbcoin' => $user_info['bbcoin'],
                                'user_money' => $user_info['user_money']
                            ));
                        }
                    }
                }
        */
        //var_dump($first_store);
    }

    public function return_owns_money() {

        return false;
        /*
                $order_delivery_note_list = _model('order_delivery_note')->getList(array(1 => 1));
                $order_delivery_note_list = api_helper::array_to_filelds_assoc('order_id', $order_delivery_note_list);
                $order_ids = array_keys($order_delivery_note_list);
                $order_list = _model('order')->getList(array('id' => $order_ids));

                $owned_order_list = array();
                $charge_order_list = array();
                foreach ($order_list as $order_detail) {
                    $store_info = store_helper::get_store($order_detail['store_id']);
                    $agency_info = user_helper::get_user_info($order_detail['agency_id']);

                    if ($store_info['is_allow_credit'] == 1 && $order_detail['is_ious'] == 1 && $order_detail['is_pay'] == 1) {
                        if ($order_detail['delivery_note_status'] == 4) {
                            $owned_order_list[] = array($order_detail['id'], $order_detail['agency_id'], $store_info['title']);

                            $order_delivery_note = $order_delivery_note_list[$order_detail['id']];

                            $set_delivery = array();

                            // 下发余额
                            if ($order_delivery_note['is_return_cash'] != 1) {
                                $return_cash = $order_delivery_note['return_cash'];
                                $extra = '白条支付自动返还，金额 ' . $return_cash . ';';

                                // user_id: 合伙人
                                user_money_helper::change_user_money(array(
                                    'user_id' => $order_detail['agency_id'],
                                    'user_money' => $return_cash,
                                    'order_id' => $order_detail['id'],
                                    'store_id' => $order_detail['store_id'],
                                    'res_id' => 10,
                                    'role_id' => user_helper::get_admin_role_id(),
                                    'admin_id' => user_helper::get_admin_id(),
                                    'extra' => $extra,
                                ));

                                $set_delivery['return_cash_time'] = datetime(); // 下发余额时间
                                $set_delivery['is_return_cash'] = 1; // 标识已下发余额
                            }

                            if ($order_delivery_note['is_return_bbcoin'] != 1) {
                                $return_bbcoin = $order_delivery_note['return_bbcoin'];
                                $extra = '白条支付自动返还，数量 ' . $return_bbcoin . ';';

                                // user_id: 合伙人
                                bbcoin_helper::change_bbcoin(array(
                                    'user_id' => $order_detail['agency_id'],
                                    'bbcoin' => $return_bbcoin,
                                    'order_id' => $order_detail['id'],
                                    'store_id' => $order_detail['store_id'],
                                    'res_id' => 10,
                                    'role_id' => user_helper::get_admin_role_id(),
                                    'admin_id' => user_helper::get_admin_id(),
                                    'extra' => $extra,
                                ));

                                $set_delivery['return_bbcoin_time'] = datetime(); // 下发的抱抱币时间
                                $set_delivery['is_return_bbcoin'] = 1; // 标识已下发抱抱币
                            }


                            echo join(',', array(
                                order_helper::order_id_encode($order_detail['id']),
                                $store_info['title'] . $store_info['title_branch'],
                                $agency_info['realname'],
                                '开启账期成功',
                                $order_delivery_note['return_cash'],
                                $order_delivery_note['return_bbcoin']
                            ));

                            if (!empty($set_delivery)) {
                                _model('order_delivery_note')->update(array('order_id' => $order_detail['id']), $set_delivery);
                            }
                            _model('order')->update(array('id' => $order_detail['id']), array('is_ious' => 1));

                        } elseif ($order_detail['delivery_note_status'] < 2) {
                            $charge_order_list[] = array($order_detail['id'], $order_detail['agency_id'], $store_info['title']);
                            echo join(',', array(
                                order_helper::order_id_encode($order_detail['id']),
                                $store_info['title'] . $store_info['title_branch'],
                                $agency_info['realname'],
                                '开启账期成功',
                            ));
                            _model('order')->update(array('id' => $order_detail['id']), array('is_pay' => 0, 'is_ious' => 1));
                        }

                        echo '<br/>';
                    }
                }
        */
    }
}